/**
  ******************************************************************************
  * @file 	zbProtocol.h 
  * @brief  Protocol header file 
  ******************************************************************************
  

Name					Size (bytes)	Description
Start of packet / ID	1			0x01 (SOH) default
Transaction Number		1			repeats master
Command					1			See command bytes and their arguments below
Flags					1			Flags to show status of packet, see below definition
Size					2			MSB first size of packet payload 
Payload					Variable	Payload or command arguments
CRC						2			CRC of entire packet, including header
*/

// commands
#define cmd_read 0x00
#define cmd_write 0x01

#define cmd_last = cmd_write;
  
#ifndef __zbProtocol_H
#define __zbProtocol_H

//#include "unitTest.h" // some defines are in here, todo move protocol defines out !
#include "stm32f10x_crc.h"
#include "unitTest.h"

// defines
#define hdrSize			6
#define	crcSize			4
#define maxPayloadSize 	1024
#define pktBuffSize 	(maxPayloadSize + hdrSize + crcSize)*2 // enough for two packets with payload 1024


// global vars
extern u8 g_PktRXBuff[pktBuffSize]; // make this smaller ? 
extern u8 g_PktTXBuff[pktBuffSize];
extern u8 dataState;
extern u32 botStatus;
//extern u32 status; // in unittest.c

// file scope vars 
static u8 	ID 			= 0x01;
static u8	PktBuf		= 0; // we are on 0 or 1, double buffer

 

// prototypes
u8 createHeader(u8 headerLocation[], u8 TXN, u8 command, u8 flags, u16 size );	// create header
void appendCRC(u8 dataBuffer[], u16 size); // append crc after size
void putPacketInBuffer(u8 payload[], u8 TXN, u8 command, u8 flags, u16 size); // pointless now ? 
u8 checkPacket(u8 packet[], u16 size);
u16 processPacket(u8 packet[], u16 size);
u16 createResponse(u8 packet[], u16 payloadSize, u8 flags);

// todo move these to utility class
void u32ToBytes(u32 value, u8 buffer[]);
u32 bytesToU32(u8 buffer[]);
u16 bytesToU16(u8 buffer[]);

#endif
